﻿using aier.dataaccess.model;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.dataaccess.dapper
{
    public class CustomerUserDal
    {
        public DapperHelper dapperHelper { get; set; }

       
        /// <summary>
        /// 根据微信公众平台ID和用户对应OPENID查询客户绑定信息(select(String appId,String openId))
        /// </summary>
        /// <param name="appId">微信公众平台APPID</param>
        /// <param name="openId">微信用户对应微信公众平台唯一标识</param>
        /// <returns></returns>
        public CustomerUserView QueryCustomerByAppIdAndOpenId(string appId,string openId) {
            if (string.IsNullOrWhiteSpace(appId)||string.IsNullOrWhiteSpace(openId)) {
                return null;
            }
            return dapperHelper.ExecueWithoutTransaction<CustomerUserView>(a =>
            {
                string sql = @"select  * from t_user /**where**/";
                SqlBuilder sqlBuilder = new SqlBuilder();
                var sqlTemplate = sqlBuilder.AddTemplate(sql);
                sqlBuilder.Where("APPID = @appId", new { appId = appId });
                sqlBuilder.Where("OPENID = @openId", new { openId = openId });
                return a.Query<CustomerUserView>(sqlTemplate.RawSql, sqlTemplate.Parameters).First();
            });
        }

        /// <summary>
        /// 添加客户信息
        /// </summary>
        /// <param name="customerUserView"></param>
        /// <returns></returns>
        public bool SaveCustomerUser(CustomerUserView customerUserView) {
            if (customerUserView == null)
            {
               
                return false;
            }
            return dapperHelper.ExecueWithoutTransaction<bool>(a => {
                string sql = @"insert into t_user( USERID, USERNAME, PASSWORD, APPID, OPENID, STATE, CUSTOMERID, INTIME, MODTIME
                                                         ) values(
                                                         @USERID, @USERNAME, @PASSWORD, @APPID, @OPENID, @STATE, @CUSTOMERID, @INTIME, @MODTIME
                                                        )";
                SqlBuilder sqlbuilder = new SqlBuilder();
                var sqlTemplate = sqlbuilder.AddTemplate(sql);
                a.Execute(sqlTemplate.RawSql, customerUserView);
                return true;
            });
        }
    }
}
